package com.dzl.enterprise;

import com.alibaba.fastjson.JSONObject;
import com.dzl.admin.pojo.SysAdmin;
import com.dzl.admin.service.SysAdminService;
import com.dzl.asynthread.SyncInitDataServiceImpl;
import com.dzl.base.controller.BaseController;
import com.dzl.card.pojo.query.WelfareCardQuery;
import com.dzl.constant.CommonConstants;
import com.dzl.enterprise.pojo.WelfareEnterprise;
import com.dzl.enterprise.pojo.dto.WelfareEnterpriseSaveDto;
import com.dzl.enterprise.pojo.vo.WelfareEnterpriseVo;
import com.dzl.enterprise.service.WelfareEnterpriseService;
import com.dzl.goods.pojo.WelfareGoods;
import com.dzl.util.*;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Maps;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 企业管理(WelfareEnterprise)表控制层
 * @author liuguanjun
 * @since 2021-09-06 14:29:41
 */
@RestController
@RequestMapping("welfareEnterprise")
public class WelfareEnterpriseController extends BaseController {


    @Autowired
    private WelfareEnterpriseService service;
    @Autowired
    private SysAdminService sysAdminService;
    @Autowired
    private SyncInitDataServiceImpl syncInitDataServiceImpl;

    /**
     * 分页查询Saas账号列表
     * @date 2021-09-06 15:15:00
     * @author liuguanjun
     * @param query 查询条件
     */
    @GetMapping("selectWelfareEnterpriseListPage")
    public PageUtils<WelfareEnterpriseVo> selectWelfareEnterpriseListPage(WelfareCardQuery query) {
        PageInfo<WelfareEnterpriseVo> pageInfo = service.selectWelfareEnterpriseListPage(query);
        return new PageUtils<>(pageInfo);
    }

    /**
     * 查询已开通独立配置的线上客户列表
     * @date 2022-01-12 09:47:00
     * @author liuguanjun
     */
    @GetMapping("selectWelfareEnterpriseList")
    public JsonResult<List<WelfareEnterprise>> selectWelfareEnterpriseList() {
        WelfareEnterprise query = new WelfareEnterprise();
        query.setStatus(CommonConstants.YES);
        query.setIsOpenapp(CommonConstants.YES);
        List<WelfareEnterprise> list_ = service.select(query);
        //List<WelfareEnterprise> list_ = new ArrayList<>();
       /* WelfareEnterprise welfareEnterprise = new WelfareEnterprise();
        welfareEnterprise.setId(0);
        welfareEnterprise.setEnterpriseName("福力兔");
        list_.add(welfareEnterprise);
        list_.addAll(list);*/
        return JsonResult.success(list_);
    }

    /**
     * 根据id查询企业信息
     * @date 2021-09-06 15:15:00
     * @author liuguanjun
     * @param id 企业id
     */
    @GetMapping("selectWelfareEnterpriseById")
    public JsonResult<WelfareEnterpriseVo> selectWelfareEnterpriseById(@RequestParam("id") Integer id) {
        return JsonResult.success(service.selectWelfareEnterpriseById(id));
    }

    /**
     * 保存Saas账号：添加或修改基本信息
     * @date 2021-09-06 14:15:05
     * @author liuguanjun
     * @param welfareEnterprise Saas账号数据对象
     */
    @PostMapping("saveWelfareEnterprise")
    public JsonResult<String> saveWelfareEnterprise(@RequestBody WelfareEnterpriseSaveDto welfareEnterprise) {
        if(EmptyUtils.isEmpty(welfareEnterprise.getEnterpriseName())) {
            return JsonResult.paramErrWithMsg("公司名称不能为空");
        }
        if(EmptyUtils.isEmpty(welfareEnterprise.getEnterpriseAbbreviation())) {
            return JsonResult.paramErrWithMsg("公司简称不能为空");
        }
        welfareEnterprise.setUpdateType(0);
        Integer count = service.saveWelfareEnterprise(welfareEnterprise);
        return count > 0 ? JsonResult.success("保存成功") : JsonResult.paramErrWithMsg("保存失败！");
    }

    /**
     * 删除企业客户
     * @param id 主键id
     * @author liuguanjun
     * @since 2022-02-14 15:33:38
     **/
    @DeleteMapping(value = "deleteWelfareEnterprise")
    public JsonResult<Integer> deleteWelfareEnterprise(Integer id) {
        if (EmptyUtils.isEmpty(id)) {
            return JsonResult.paramErrWithMsg("参数错误");
        }
        WelfareEnterprise welfareEnterprise = service.selectByPrimaryKey(id);
        if(EmptyUtils.isEmpty(welfareEnterprise)){
            return JsonResult.failWithMsg("该企业客户数据不存在");
        }
        if(welfareEnterprise.getIsApi()==CommonConstants.YES){
            return JsonResult.failWithMsg("该企业已开启api配置，不能删除，请先关闭api配置");
        }
        if(welfareEnterprise.getIsOpenapp()==CommonConstants.YES){
            return JsonResult.failWithMsg("该企业已开启独立部署，不能删除，请先关闭独立部署");
        }
        welfareEnterprise.setStatus(CommonConstants.NO);
        welfareEnterprise.setUpdateTime(new Date());
        return JsonResult.success(service.updateByPrimaryKeySelective(welfareEnterprise));
    }


    /**
     * 兔福宝配置修改
     * @date 2022-01-08 11:22:05
     * @author liuguanjun
     * @param welfareEnterprise Saas账号数据对象
     */
    @PostMapping("updateTfbConfig")
    public JsonResult<String> updateTfbConfig(@RequestBody WelfareEnterpriseSaveDto welfareEnterprise) {
        if(EmptyUtils.isEmpty(welfareEnterprise.getId())) {
            return JsonResult.paramErrWithMsg("线上客户id不能传空");
        }
        if(welfareEnterprise.getIsTfb()==null) {
            return JsonResult.paramErrWithMsg("isTfp不能传空");
        }
        if(welfareEnterprise.getIsTfb()==CommonConstants.YES){
            if(EmptyUtils.isEmpty(welfareEnterprise.getAccount())) {
                return JsonResult.paramErrWithMsg("注册账号不能为空");
            }
            if(EmptyUtils.isEmpty(welfareEnterprise.getDomain())) {
                return JsonResult.paramErrWithMsg("后台链接不能为空");
            }
        }
        welfareEnterprise.setUpdateType(1);
        Integer count = service.saveWelfareEnterprise(welfareEnterprise);
        return count > 0 ? JsonResult.success("保存成功") : JsonResult.paramErrWithMsg("保存失败！");
    }

    /**
     * 独立部署配置修改
     * @date 2022-01-08 11:22:05
     * @author liuguanjun
     * @param welfareEnterprise Saas账号数据对象
     */
    @PostMapping("updateAppConfig")
    public JsonResult<Integer> updateAppConfig(@RequestBody WelfareEnterpriseSaveDto welfareEnterprise) {
        if(EmptyUtils.isEmpty(welfareEnterprise.getId())) {
            return JsonResult.paramErrWithMsg("线上客户id不能传空");
        }
        if(welfareEnterprise.getIsOpenapp()==null) {
            return JsonResult.paramErrWithMsg("isOpenapp不能传空");
        }
        if(EmptyUtils.isEmpty(welfareEnterprise.getPhone())) {
            return JsonResult.paramErrWithMsg("注册手机号不能为空");
        }
        if(welfareEnterprise.getIsOpenapp()==CommonConstants.YES){
            if(welfareEnterprise.getAppRuleConfig() == null) {
                return JsonResult.paramErrWithMsg("请选择配置规则");
            }
            if(EmptyUtils.isEmpty(welfareEnterprise.getAppname())) {
                return JsonResult.paramErrWithMsg("小程序名称不能为空");
            }
            if(EmptyUtils.isEmpty(welfareEnterprise.getAppid())) {
                return JsonResult.paramErrWithMsg("小程序id不能为空");
            }
            if(EmptyUtils.isEmpty(welfareEnterprise.getAppsecret())) {
                return JsonResult.paramErrWithMsg("小程序密钥不能为空");
            }else{
                if(welfareEnterprise.getAppsecret().length()!=32){
                    return JsonResult.paramErrWithMsg("小程序密钥长度必须32位");
                }
            }
            if(EmptyUtils.isEmpty(welfareEnterprise.getMchId())) {
                return JsonResult.paramErrWithMsg("小程序商户id不能为空");
            }
            if(EmptyUtils.isEmpty(welfareEnterprise.getMchKey())) {
                return JsonResult.paramErrWithMsg("小程序商户密钥不能为空");
            }else{
              if(welfareEnterprise.getMchKey().length()!=32){
                  return JsonResult.paramErrWithMsg("小程序商户密钥长度必须32位");
              }
            }
        }
        welfareEnterprise.setUpdateType(3);
        Integer count = service.saveWelfareEnterprise(welfareEnterprise);
        //若选择自行配置，且没有同步过平台商品，则同步平台商品
        if(EmptyUtils.isNotEmpty(welfareEnterprise.getAppRuleConfig()) && welfareEnterprise.getAppRuleConfig() == CommonConstants.YES){
            WelfareEnterprise welfareEnterprise_ = service.selectByPrimaryKey(welfareEnterprise.getId());
            if(EmptyUtils.isEmpty(welfareEnterprise_.getAppRuleConfig())){
                syncInitDataServiceImpl.syncGoodsListToCompany(welfareEnterprise.getId());
            }
        }
        return JsonResult.success(count);
    }



    /**
     * 开启API权限
     * @date 2021-11-23 16:11:00
     * @author liuguanjun
     * @param id 企业id
     */
    @GetMapping("openWelfareEnterpriseApi")
    public JsonResult<Map> openWelfareEnterpriseApi(@RequestParam("id") Integer id) {
        return JsonResult.success(service.openWelfareEnterpriseApi(id));
    }

    /**
     * 获取saas小程序主页配置
     * @date 2021-09-06 18:04:05
     * @author liuguanjun
     */
    @GetMapping("getAppletHomePagePoster")
    public JsonResult<Map> getAppletHomePagePoster() {
        Map map = Maps.newHashMapWithExpectedSize(2);
        map.put("posterUrl","");
        map.put("isDisplayHome", CommonConstants.NO);
        WelfareEnterprise welfareEnterprise = service.selectByPrimaryKey(-1);
        if(EmptyUtils.isNotEmpty(welfareEnterprise)){
            map.put("posterUrl",ImageUtils.setImageSrc(welfareEnterprise.getEnterpriseLogo()));
            map.put("isDisplayHome", welfareEnterprise.getIsDisplayHome());
        }
        return JsonResult.success(map);
    }

    /**
     * 设置saas小程序主页配置是否显示
     * @date 2021-09-06 18:04:05
     * @author liuguanjun
     * @param param Saas账号数据对象
     */
    @PostMapping("setAppletHomePagePoster")
    public JsonResult<Integer> saveWelfareEnterprise(@RequestBody JSONObject param) {
        String posterImgUrl = param.getString("posterImgUrl");//首页海报页地址
        Integer isDisplayHome = param.getInteger("isDisplayHome");//是否首页显示：0否 1是
        if(EmptyUtils.isEmpty(posterImgUrl)) {
            return JsonResult.paramErrWithMsg("请上传小程序主页图片");
        }
        WelfareEnterprise welfareEnterprise = new WelfareEnterprise();
        welfareEnterprise.setId(-1);
        welfareEnterprise.setEnterpriseLogo(posterImgUrl);
        welfareEnterprise.setIsDisplayHome(isDisplayHome);
        welfareEnterprise.setUpdateTime(new Date());
        return JsonResult.success(service.updateByPrimaryKeySelective(welfareEnterprise));
    }


    /**
     * saas账号重置密码
     * @date 2021-09-06 14:15:05
     * @author liuguanjun
     * @param param 账号id等传参
     */
    @PostMapping("resetPassword")
    public JsonResult<Integer> resetPassword(@RequestBody JSONObject param) {
        Integer accountId = param.getInteger("accountId"); //账号id
        if(EmptyUtils.isEmpty(accountId)) {
            return JsonResult.paramErrWithMsg("缺少参数[accountId:账号id]！");
        }
        SysAdmin sysAdmin = new SysAdmin();
        sysAdmin.setId(accountId);
        sysAdmin.setPassword(PasswordUtil.AESEncrypt("123456"));
        return JsonResult.success(sysAdminService.updateByPrimaryKeySelective(sysAdmin));
    }


    /**
     * 设置是否首页显示
     * @date 2021-09-07 16:53:05
     * @author liuguanjun
     * @param param 企业id 等参数
     */
    @PostMapping("setHomePageDisplay")
    public JsonResult<Integer> setHomePageDisplay(@RequestBody JSONObject param) {
        Integer id = param.getInteger("id");//企业id
        Integer isDisplayHome = param.getInteger("isDisplayHome"); //是否显示：0否 1是
        if(EmptyUtils.isEmpty(id)) {
            return JsonResult.paramErrWithMsg("缺少参数[id:企业id]！");
        }
        WelfareEnterprise welfareEnterprise = new WelfareEnterprise();
        welfareEnterprise.setId(id);
        welfareEnterprise.setIsDisplayHome(isDisplayHome);
        welfareEnterprise.setUpdateTime(new Date());
        return JsonResult.success(service.updateByPrimaryKeySelective(welfareEnterprise));
    }


}
